package com.sinaapp.gavinzhang.ConcurrentProgramming;

import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 延时任务
 * 重复任务
 * Created by gavin on 15-9-4.
 */
public class Scheduled {
    public static void main(String[] args) throws InterruptedException {
        ScheduledThreadPoolExecutor executor = (ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(1);
        for (int i = 0; i < 5; i++) {
            ScheduledCallTest task = new ScheduledCallTest();
            //executor.schedule(task,i+1, TimeUnit.SECONDS);
        }
        executor.scheduleAtFixedRate(new ScheduledCallTest(),0,1, TimeUnit.SECONDS);
        Thread.sleep(5000);
        executor.shutdown();

    }
}

class ScheduledCallTest implements Callable<String>,Runnable{
    public String call() throws Exception {
        String message = Thread.currentThread().getName()+" finish "+System.currentTimeMillis();
        System.out.println(message);

        return message;
    }

    public void run() {
        String message = Thread.currentThread().getName()+" finish "+System.currentTimeMillis();
        System.out.println(message);
    }
}
